package hc.bre.clas

import static org.junit.jupiter.api.Assertions.*

import hc.bre.clas.ClasResult
import hc.bre.clas.yesno.*
import org.junit.jupiter.api.Test

@groovy.transform.CompileStatic
class YesNoTest {

	@Test
	void create() {
		YesNoDefinition ruleDef = YesNoDefinition.create()

		assert ruleDef.list.size() == 2
	}

	String jsonText='''
{
  "rid" : 167,
  "rtype" : "rule:clas:yesno",
  "rname" : "逻辑是否分类2",
  "rpath" : "xsxdjc/class9/LuoJiShiFuFenLei2",
  "rversion" : 8,
  "script" : "true",
  "rmemo" : "",
  "rtest" : "{}",
  "list" : [ {
    "id" : 1,
    "code" : "yes",
	"range":"true",
    "name" : "是",
	"otherflag":0
  }, {
    "id" : 0,
    "code" : "no",
	"range":"false",
    "name" : "否",
	"otherflag":1
  } ]
}
'''
	@Test
	@groovy.transform.CompileDynamic
	void load() {

		YesNoDefinition ruleDef = new YesNoDefinition()
		ruleDef.load(hc.Util.jsonRead(jsonText));

		println ruleDef.buildRuleScript();

		ClasResult result =  ruleDef.buildRuleEvaluate([:]).result;
		assert result.clas.id == 1
		assert result.clas.code == 'yes'
	}

	@Test
	@groovy.transform.CompileDynamic
	void buildAndEvaluate() {
		YesNoDefinition ruleDef = YesNoDefinition.create();
		ruleDef.rpath = "prj/Hello"
		ruleDef.script ='false'

		assert ruleDef.buildRuleEvaluate([:]).result.clas.id == 0

		ruleDef.script ='1==1'

		assert ruleDef.buildRuleEvaluate([:]).result.clas.id == 1
	}
}
